using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace KBE_HotFix.UI
{
    public class UIObject
    {
        
        private static Dictionary<string, UIObject> UIInCurrentScene = new Dictionary<string, UIObject>();
        public static UIObject Root = new UIObject();
        public static UIObject Center = new UIObject();
        public static UIObject Top = new UIObject();
        public static UIObject TopLeft = new UIObject();
        public static UIObject TopRight = new UIObject();
        public static UIObject Left = new UIObject();
        public static UIObject Right = new UIObject();
        public static UIObject Bottom = new UIObject();
        public static UIObject BottomLeft = new UIObject();
        public static UIObject BottomRight = new UIObject();
        public UIBehaviour uiInstance;
        public Transform transform => uiInstance.transform;
        public string Tag;

        public static UIObject GetUIObject(string Tag)
        {
            UIObject obj;
            if(UIInCurrentScene.TryGetValue(Tag, out obj))
            {
                return obj;
            }
            return null;
        }

        public static void SetRoot(UIBehaviour root)
        {
            Root.uiInstance = root;
            var trans = Root.transform.Find("Center");
            if(trans != null)
            {
                Center.SetInstance(trans);
            }

            trans = Root.transform.Find("Top");
            if(trans != null)
            {
                Top.SetInstance(trans);
            }

            trans = Root.transform.Find("TopLeft");
            if(trans != null)
            {
                TopLeft.SetInstance(trans);
            }

            trans = Root.transform.Find("TopRight");
            if(trans != null)
            {
                TopRight.SetInstance(trans);
            }

            trans = Root.transform.Find("Left");
            if(trans != null)
            {
                Left.SetInstance(trans);
            }

            trans = Root.transform.Find("Right");
            if(trans != null)
            {
                Right.SetInstance(trans);
            }

            trans = Root.transform.Find("Bottom");
            if(trans != null)
            {
                Bottom.SetInstance(trans);
            }

            trans = Root.transform.Find("BottomLeft");
            if(trans != null)
            {
                BottomLeft.SetInstance(trans);
            }

            trans = Root.transform.Find("BottomRight");
            if(trans != null)
            {
                BottomRight.SetInstance(trans);
            }
            
            UIInCurrentScene.Clear();
        }

        public static void CreateObject(UIBehaviour obj, string tag)
        {
            new UIObject(obj, tag);
        }

        public UIObject()
        {
        }

        public UIObject(UIBehaviour obj, string tag)
        {
            Tag = tag;
            UIInCurrentScene.Add(tag, this);
            SetInstance(obj);
        }

        public void SetInstance(UIBehaviour obj)
        {
            uiInstance = obj;
        }

        public void SetInstance(Transform trans)
        {
            var obj = trans.GetComponent<UIBehaviour>();
            SetInstance(obj);
        }

        public void RegisterOut(string name, string method)
        {
            KBE_HotFix.Event.registerOut(name, this, method);
        }

        public void DeregisterAll()
        {
            KBE_HotFix.Event.deregisterOut(this);
        }
    }
}
